/*
 *    Copyright 2008-2009 The Digester Tools Team
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.digestertools.annotations.providers;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.digester.SetPropertiesRule;

import com.digestertools.annotations.AnnotationRuleProvider;

/**
 * 
 * @author Simone Tripodi
 * @version $Id: SetPropertyRuleProvider.java 357 2009-12-16 20:00:36Z simone.tripodi $
 */
public final class SetPropertyRuleProvider implements AnnotationRuleProvider<SetPropertiesRule> {

    private final Map<String, String> aliases = new HashMap<String, String>();

    public void addAlias(String attributeName, String propertyName) {
        if (attributeName == null || attributeName.length() == 0) {
            attributeName = propertyName;
        }

        this.aliases.put(attributeName, propertyName);
    }

    public SetPropertiesRule newRule() {
        String[] attributeNames = new String[this.aliases.size()];
        String[] propertyNames = new String[attributeNames.length];

        int i = 0;
        for (Entry<String, String> aliasEntry : this.aliases.entrySet()) {
            attributeNames[i] = aliasEntry.getKey();
            propertyNames[i] = aliasEntry.getValue();
            i++;
        }

        return new SetPropertiesRule(attributeNames, propertyNames);
    }

}
